﻿using UnityEngine;
using UnityEditor;

namespace SocketWeaver.FixedMath.CustomUnityEditor
{
    [CustomEditor(typeof(FTransform))]
    //[CanEditMultipleObjects]
    public class FTransformCustomInspector : Editor
    {
        SerializedProperty position;
        SerializedProperty rotation;
        SerializedProperty scale;
        SerializedProperty showDebugInfo;
        SerializedProperty interpolation;

        private void OnEnable()
        {
            position = serializedObject.FindProperty("_localPosition");
            rotation = serializedObject.FindProperty("_localRotation");
            scale = serializedObject.FindProperty("_localScale");
            showDebugInfo = serializedObject.FindProperty("editor_showDebugInfo");
            interpolation = serializedObject.FindProperty("interpolation");
        }

        public override void OnInspectorGUI()
        {
            //DrawDefaultInspector();
            serializedObject.Update();

            EditorGUILayout.PropertyField(interpolation, new GUIContent("Interpolation"));

            EditorGUI.BeginDisabledGroup(true);
            EditorGUILayout.PropertyField(position, new GUIContent("Position"));
            EditorGUILayout.PropertyField(rotation, new GUIContent("Rotation"));
            EditorGUILayout.PropertyField(scale, new GUIContent("Scale"));
            EditorGUI.EndDisabledGroup();

            EditorGUILayout.PropertyField(showDebugInfo, new GUIContent("Show Debug Info"));

            serializedObject.ApplyModifiedProperties();

            FTransform ft = (FTransform)target;

            //if (ft.transform.hasChanged)
            //{
            //    ft.transform.hasChanged = false;
            //    ft.localPosition = (FVector3)ft.transform.localPosition;
            //    ft.localRotation = (FQuaternion)ft.transform.localRotation;
            //    ft.localScale = (FVector3)ft.transform.localScale;
            //    EditorUtility.SetDirty(ft);
            //}

            if (ft.editor_showDebugInfo)
            {
                GUIStyle green = new GUIStyle();
                GUIStyle red = new GUIStyle();

                green.normal.textColor = Color.green;
                red.normal.textColor = Color.red;

                GUILayout.Label($"Position\n{ft.transform.position.ToString("F4")}\n{ft.position}", ft.position.Equals(ft.transform.position) ? green : red);
                GUILayout.Label($"Rotation\n{ft.transform.rotation.ToString("F4")}\n{ft.rotation}", ft.rotation.Equals(ft.transform.rotation) ? green : red);
                GUILayout.Label($"EulerAngles\n{ft.transform.eulerAngles.ToString("F4")}\n{ft.eulerAngles}", ft.eulerAngles.AngleEquals(ft.transform.eulerAngles) ? green : red);
                GUILayout.Label($"Scale\n{ft.transform.localScale.ToString("F4")}\n{ft.localScale}", ft.localScale.Equals(ft.transform.localScale) ? green : red);
                GUILayout.Label($"LocalToWorld\n{ft.transform.localToWorldMatrix.ToString("F4")}\n{ft.localToWorldMatrix}", ft.localToWorldMatrix.Equals(ft.transform.localToWorldMatrix) ? green : red);
                GUILayout.Label($"WorldToLocal\n{ft.transform.worldToLocalMatrix.ToString("F4")}\n{ft.worldToLocalMatrix}", ft.worldToLocalMatrix.Equals(ft.transform.worldToLocalMatrix) ? green : red);
            }
        }
    }

}

